package com.meituan.catering.management.shop.api.http.controller;

import com.meituan.catering.management.shop.api.http.model.request.*;
import com.meituan.catering.management.shop.api.http.model.response.ShopDetailHttpResponse;
import com.meituan.catering.management.shop.api.http.model.response.ShopPageHttpResponse;
import com.meituan.catering.management.shop.biz.model.ShopBO;
import com.meituan.catering.management.shop.biz.model.common.PageBO;
import com.meituan.catering.management.shop.biz.model.common.UserContextBO;
import com.meituan.catering.management.shop.biz.model.converter.ShopBOConverter;
import com.meituan.catering.management.shop.biz.model.converter.ShopVOConverter;
import com.meituan.catering.management.shop.biz.model.converter.UserContextBOConverter;
import com.meituan.catering.management.shop.biz.service.ShopBizBusinessNoService;
import com.meituan.catering.management.shop.biz.service.ShopBizOperateService;
import com.meituan.catering.management.shop.biz.service.ShopBizQueryService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import javax.validation.constraints.Digits;


/**
 * @Author:ale
 * @Date 2022/4/22 16:49
 * @Version 1.0
 */
@Api(tags = "美团餐饮门店管理")
@RestController
@RequestMapping("/shop")
public class ShopController {

    @Resource
    private ShopBizOperateService shopBizOperateService;

    @Resource
    private ShopBizBusinessNoService shopBizBusinessNoService;

    @Resource
    private ShopBizQueryService shopBizQueryService;


    @ApiOperation("通过商户号查看门店信息")
    @GetMapping("/{businessNo}")
    public ShopDetailHttpResponse findByBusinessNo(
            @ApiParam("") @Valid @Digits(integer = 10, fraction = 0) @PathVariable String businessNo) {
            ShopBO shopBO = shopBizBusinessNoService.findByBusinessNo(businessNo);
        return ShopVOConverter.fromBO(shopBO);
    }

    @ApiOperation("创建门店")
    @PostMapping("/create")
    public ShopDetailHttpResponse create(
            @ApiParam("用户上下文") @Valid UserContextHttpRequest userContext,
            @ApiParam("门店信息") @Valid @RequestBody CreateShopHttpRequest request) {
        ShopBO shopBO = shopBizOperateService.create(fromHttpRequest(userContext), request);
        return ShopVOConverter.fromBO(shopBO);
    }

    @ApiOperation("按照商户号更新门店信息")
    @PutMapping("/{businessNo}")
    public ShopDetailHttpResponse update(
            @ApiParam("门店商户号") @PathVariable String businessNo,
            @ApiParam("更新门店信息") @Valid @RequestBody UpdateShopHttpRequest request) {
        ShopBO shopBO = shopBizOperateService.update(businessNo,request);
        return ShopVOConverter.fromBO(shopBO);
    }

    @ApiOperation("分页搜索全部门店信息")
    @PostMapping("/searchForPage")
    public ShopPageHttpResponse searchForPage(
            @ApiParam("搜索请求体") @Valid @RequestBody SearchShopHttpRequest request) {
        PageBO<ShopBO> pageBO = shopBizQueryService.searchForPage(request);
        return ShopVOConverter.fromPageBO(pageBO);
    }

    /**
     * TODO：功能不完整，有bug，在递归查询子门店的条数时陷入死循环，未解决
     * @param userContext
     * @param request
     * @return
     */
    @ApiOperation("连锁管理")
    @PostMapping("/searchForPagePrivileged")
    public ShopPageHttpResponse searchForPageTwo(
            @ApiParam("用户上下文")@Valid UserContextHttpRequest userContext,
            @ApiParam("搜索请求体") @Valid @RequestBody SearchShopHttpRequest request) {
        PageBO<ShopBO> pageBO = shopBizQueryService.searchForPageTwo(UserContextBOConverter.fromHttpRequest(userContext), request);
        return ShopVOConverter.fromPageBO(pageBO);
    }


    @ApiOperation("精确搜索")
    @PostMapping
    public ShopPageHttpResponse queryForPage(
            @ApiParam("搜索请求体") @Valid @RequestBody SearchShopHttpRequest request) {
        PageBO<ShopBO> pageBO = shopBizQueryService.queryForPage(request);
        return ShopVOConverter.fromPageBO(pageBO);
    }


    @ApiOperation("按照商户号启用一家门店")
    @PostMapping("/open/{businessNo}")
    public ShopDetailHttpResponse open(
            @ApiParam("门店业务号") @PathVariable String businessNo,
            @ApiParam("启用请求体") @Valid @RequestBody OpenShopHttpRequest request) {
        ShopBO shopBO = shopBizOperateService.open(businessNo,request);
        return ShopVOConverter.fromBO(shopBO);
    }

    @ApiOperation("按照商户号关闭一家门店")
    @PostMapping("/close/{businessNo}")
    public ShopDetailHttpResponse close(
            @ApiParam("门店业务号") @PathVariable String businessNo,
            @ApiParam("关闭请求体") @Valid @RequestBody CloseShopHttpRequest request) {
        ShopBO shopBO = shopBizOperateService.close(businessNo,request);
        return ShopVOConverter.fromBO(shopBO);
    }

    private Long fromHttpRequest(UserContextHttpRequest userContext) {
        Long tenantId = userContext.getTenantId();
        return tenantId;
    }

}
